package com.webber.demo.common.utils;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
/**
 * 
 * <p>Title: SendRquestUtil.java</p>
 * <p>Description:发送请求工具类</p>
 * <p>Copyright: Copyright (c) 2017</p>
 * <p>Company: 博达软件</p>
 * <p>ProjectName: partyconstruction</p>
 * @Package com.webber.demo.common.utils 
 * @author JobsZhang
 * @date 2017年8月29日 下午4:09:04
 * @version partyconstruction 1.0
 */
public class SendRquestUtil {
	/**
	 * 请求地址
	 */
	private String actionURL=null;
	/**
	 * 请求参数集合
	 */
	private HashMap<String, Object> parameters=null;
	
	
	public String getActionURL() {
		return actionURL;
	}


	public void setActionURL(String actionURL) {
		this.actionURL = actionURL;
	}


	public HashMap<String, Object> getParameters() {
		return parameters;
	}


	public void setParameters(HashMap<String, Object> parameters) {
		this.parameters = parameters;
	}

	
	public SendRquestUtil(String actionURL) {
		this.actionURL = actionURL;
	}


	/**
	 * 
	 * <p>Title: executePostByUsual</p>
	 * <p>Description: 发送post请求，把响应结果用String返回 </p>
	 * @param parameters 请求参数
	 * @return
	 * @author JobsZhang
	 * @date 2017年8月29日 下午4:09:50
	 */
	public  String executePostByUsual( HashMap<String, Object> parameters){  
		this.parameters = parameters;
	    try{  
	        URL url = new URL(actionURL);  
	        HttpURLConnection connection = (HttpURLConnection)url.openConnection();  
	        //发送post请求需要下面两行  
	        connection.setDoInput(true);  
	        connection.setDoOutput(true);  
	        connection.setUseCaches(false);  
	        connection.setRequestMethod("POST");  
	        connection.setRequestProperty("Connection", "Keep-Alive");  
	        connection.setRequestProperty("Charset", "UTF-8");;  
	        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
	        connection.setConnectTimeout(5000);//jdk 1.5以上换成这个,连接超时
   	     	connection.setReadTimeout(5000);//jdk 1.5以上换成这个,读操作超时
	        //设置请求数据内容  
	        String requestContent = "";  
	        
	        if(parameters!=null){
	        	Set<String> keys = parameters.keySet();  
		        for(String key : keys){  
		           requestContent = requestContent + key + "=" + parameters.get(key) + "&"; 
		        } 
		        requestContent = requestContent.substring(0, requestContent.lastIndexOf("&"));
	        }
	        DataOutputStream ds = new DataOutputStream(connection.getOutputStream());  
	        //使用write(requestContent.getBytes())是为了防止中文出现乱码  
	        ds.write(requestContent.getBytes());  
	        ds.flush();  
	        try{  
	            //获取URL的响应  
	        	   String data = new String(IOUtils.toString(connection.getInputStream(), "UTF-8"));
	               // 断开连接 
	        	   connection.disconnect(); 
	        	   return data;
	        }catch(IOException e){  
	            e.printStackTrace();  
	            System.out.println("No response get!!!");  
	        }  
	        ds.close();  
	    }catch(IOException e){  
	        e.printStackTrace();  
	        System.out.println("Request failed!");  
	    }  
	    return null;  
	}
	/**
	 * 
	 * <p>Title: executePostByUsual</p>
	 * <p>Description:发送请求，把返回结果封装成Document对象 </p>
	 * @param encode
	 * @return
	 * @author JobsZhang
	 * @date 2017年8月29日 下午4:10:33
	 */
	public  Document executePostByUsual(String encode){  
	    try{  
	        URL url = new URL(actionURL);  
	        HttpURLConnection connection = (HttpURLConnection)url.openConnection();  
	        //发送post请求需要下面两行  
	        connection.setDoInput(true);  
	        connection.setDoOutput(true);  
	        connection.setUseCaches(false);  
	        connection.setRequestMethod("POST");  
	        connection.setRequestProperty("Connection", "Keep-Alive");  
	        connection.setRequestProperty("Charset", "UTF-8");;  
	        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
	        connection.setConnectTimeout(5000);//jdk 1.5以上换成这个,连接超时
   	     	connection.setReadTimeout(5000);//jdk 1.5以上换成这个,读操作超时
	        //设置请求数据内容  
	        String requestContent = "";  
	        if(parameters!=null){
	        	Set<String> keys = parameters.keySet();  
		        for(String key : keys){  
		           requestContent = requestContent + key + "=" + parameters.get(key) + "&"; 
		        } 
		        requestContent = requestContent.substring(0, requestContent.lastIndexOf("&"));
	        }
	        DataOutputStream ds = new DataOutputStream(connection.getOutputStream());  
	        //使用write(requestContent.getBytes())是为了防止中文出现乱码  
	        ds.write(requestContent.getBytes());  
	        ds.flush();  
	        try{  
	        	SAXReader reader = new SAXReader(); 
	        	Document document= 	reader.read(connection.getInputStream());
	        	return document;
	        }catch(IOException e){  
	            e.printStackTrace();  
	            System.out.println("No response get!!!");  
	        } catch (DocumentException e) {
	        	 System.out.println(e.getMessage());  
			}  
	        ds.close();  
	    }catch(IOException e){  
	        e.printStackTrace();  
	        System.out.println("Request failed!");  
	    }  
	    return null;  
	}
	/**
	 * 发get请求，返回字符串
	 * @return
	 */
	public String executeGetByUsual() {
		try {
			URL url = new URL(this.actionURL);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();

			connection.setDoInput(true);
			connection.setDoOutput(true);
			connection.setUseCaches(false);
			connection.setRequestMethod("GET");
			connection.setRequestProperty("Connection", "Keep-Alive");
			connection.setRequestProperty("Charset", "UTF-8");
			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			connection.setConnectTimeout(5000);
			connection.setReadTimeout(5000);
			connection.connect();
			try {
				String data = new String(IOUtils.toString(connection.getInputStream(), "UTF-8"));
				connection.disconnect();
				return data;
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("No response get!!!");

			}
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Request failed!");
		}
		return null;
	}
}
